package com.pomr.controller.common;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.util.common.ResponseData;

@ControllerAdvice
public class ExceptionLogController {
	
	private static Logger log = LoggerFactory.getLogger(ExceptionLogController.class);
	
	@ExceptionHandler({Exception.class})
	@ResponseBody
	public ResponseData handException(HttpServletRequest request ,Exception e) throws Exception{
		String message = getExceptionAllinformation_01(e);
		log.error(message);
		ResponseData pd = ResponseData.exceptionError();
		pd.putDataValue("message", e.getMessage());
		return pd;
	}
	
	public String getExceptionAllinformation_01(Exception ex) {  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        PrintStream pout = new PrintStream(out);  
        ex.printStackTrace(pout);  
        String ret = new String(out.toByteArray());  
        pout.close();  
        try {  
             out.close();
        } catch (Exception e) {  
        }  
        return ret;  
} 

}
